<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ConsultSetting extends Model
{
    protected $table = "c_consult_settings";

    /**
     * @param $data
     *添加设置
     */
    public function add($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
            $res = $this->getDetail($data);
            if (!empty($res)) {
                $res->work_id = isset($data['work_id']) ? $data['work_id'] : $res->work_id;
                $res->type = isset($data['type']) ? $data['type'] : $res->type;
                $res->consult_switch = isset($data['consult_switch']) ? $data['consult_switch'] : $res->consult_switch;
                $res->appointment_consult_switch = isset($data['appointment_consult_switch']) ? $data['appointment_consult_switch'] : $res->appointment_consult_switch;
                $res->time = isset($data['time']) ? $data['time'] : $res->time;
                $res->money = isset($data['money']) ? $data['money'] : $res->money;
                $res->doctor_id = isset($data['doctor_id']) ? $data['doctor_id'] : $res->doctor_id;
                $res->save();
            } else {
                $this->work_id = isset($data['work_id']) ? $data['work_id'] : "";
                $this->type = isset($data['type']) ? $data['type'] : "";
                $this->consult_switch = isset($data['consult_switch']) ? $data['consult_switch'] : "";
                $this->appointment_consult_switch = isset($data['appointment_consult_switch']) ? $data['appointment_consult_switch'] : "";
                $this->time = isset($data['time']) ? $data['time'] : "";
                $this->money = isset($data['money']) ? $data['money'] : "";
                $this->doctor_id = isset($data['doctor_id']) ? $data['doctor_id'] : "";
                $res = $this->save();
            }
            if ($res) {
                $result = $this->setDoctor($data);
                if ($result) {
                    DB::commit(); // 提交事务
                    return true;
                } else {
                    return '入驻失败';
                    DB::rollback(); // 回滚事务
                }
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }

    //设置工作室医师内容
    public function setDoctor($data)
    {
        $image = $this->where([['type', '=', 1], ['work_id', '=', $data['work_id']],['doctor_id', '=', $data['doctor_id']]])->first();
        $phone = $this->where([['type', '=', 2], ['work_id', '=', $data['work_id']],['doctor_id', '=', $data['doctor_id']]])->first();
        $list = [];
        $list['service_type'] = 0;
        $list['consult_type'] = 0;
        if (!empty($image)) {
            $list['imagetext_price'] = $image->money;
            $list['imagetext_time'] = $image->time;
            if ($image->appointment_consult_switch == 1) {
                $list['service_type'] = 1;
            }
            if ($image->consult_switch == 1) {
                $list['consult_type'] = 1;
            }
        }
        if (!empty($phone)) {
            $list['phone_price'] = $phone->money;
            $list['phone_time'] = $phone->time;
            if ($phone->appointment_consult_switch == 1) {
                $list['service_type'] = 2;
            }
        }
        if (!empty($image) && !empty($phone)) {
            if ($image->appointment_consult_switch == 1 && $phone->appointment_consult_switch == 1) {
                $list['service_type'] = 3;
            }
        }
        return (new Doctor())->where([['work_id', '=', $data['work_id']],['id','=',$data['doctor_id']]])->update($list);
    }
    /**
     * @param $data
     * @return mixed
     * 获取详情
     */
    public function getDetail($data)
    {
        return $this->where([['type','=',$data['type']],['work_id','=',$data['work_id']],['doctor_id','=',$data['doctor_id']]])->first();
    }

    /**
     * @param $data
     * @return mixed
     * 获取设置列表
     */
    public function getDetailNoType($data)
    {
        return $this->where([['work_id','=',$data['work_id']],['doctor_id','=',$data['work_id']]])->get();
    }
}
